package com.lantosec.hmrp.modules.morality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.modules.morality.entity.MoralityItem;
import com.lantosec.hmrp.modules.morality.mapper.MoralityItemMapper;
import com.lantosec.hmrp.modules.morality.service.IMoralityItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.morality.vo.MoralityItemVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 项目定义 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-16
 */
@Service
@Primary
@Log4j2
public class MoralityItemServiceImpl extends ServiceImpl<MoralityItemMapper, MoralityItem> implements IMoralityItemService {

    @Override
    public String queryMaxId(String parentItemId) {
        String moralityItemId = null;
        if(StringUtils.isNotEmpty(parentItemId)){
            moralityItemId = baseMapper.queryMaxIdByParentId(parentItemId);
            return StringUtils.isEmpty(moralityItemId) ? parentItemId + "001" : moralityItemId;
        }else{
            moralityItemId = baseMapper.queryMaxId();
            return StringUtils.isEmpty(moralityItemId) ? "001" : moralityItemId;
        }
    }

    @Override
    public List<MoralityItemVO> getAll(String parentItemId) {
        List<MoralityItemVO> moralityItemVOList = new ArrayList<>();
        List<MoralityItem> moralityItems = new ArrayList<>();
        if(StringUtils.isNotEmpty(parentItemId)){
            String[] parentItemIds = parentItemId.split(",");
            moralityItems = this.list(new QueryWrapper<MoralityItem>().in(MoralityItem.ColName.parent_item_id.toString(), Arrays.asList(parentItemIds))
                                                                .eq(MoralityItem.ColName.morality_item_type.toString(),1));
        }else {
            moralityItems = this.list(new QueryWrapper<MoralityItem>().isNull(MoralityItem.ColName.parent_item_id.toString())
                                                            .eq(MoralityItem.ColName.morality_item_type.toString(),1));
        }
        moralityItems.forEach(param->{
            MoralityItemVO moralityItemVO = param.convertToVO();
            List<MoralityItemVO> moralityItemVOS = getAll(param.getMoralityItemId());
            moralityItemVO.setChildren(moralityItemVOS);
            moralityItemVOList.add(moralityItemVO);
        });
        return moralityItemVOList;
    }

    @Override
    public List<MoralityItem> getAllBadItem(String parentItemId) {
        if(StringUtils.isNotEmpty(parentItemId)){
            String[] parentItemIds = parentItemId.split(",");
            return this.list(new QueryWrapper<MoralityItem>().in(MoralityItem.ColName.parent_item_id.toString(), Arrays.asList(parentItemIds))
                    .eq(MoralityItem.ColName.morality_item_type.toString(),2));
        }else {
            return this.list(new QueryWrapper<MoralityItem>().isNull(MoralityItem.ColName.parent_item_id.toString())
                    .eq(MoralityItem.ColName.morality_item_type.toString(),2));
        }
    }
}
